<template>
  <div class="container">
    <div class="app-container">
      <el-tree :expand-on-click-node="false" default-expand-all :data="depts" :props="defaultProps">
        <template #default="{data}">
          <div class="custom-tree-node">
            <el-row style="width: 100%; height: 40px;" align="middle" type="flex" justify="space-between">
              <span>{{ data.name }}</span>
              <el-col :span="3">
                <div class="right">
                  <span>{{ data.managerName }}</span>
                  <el-dropdown @command="handleCommand(data.id,$event)">
                    <span class="el-dropdown-link managerName">
                      操作<i class="el-icon-arrow-down el-icon--right" /></span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                      <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                      <el-dropdown-item command="del">删除</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </div>
              </el-col>
            </el-row>
          </div>
        </template>
      </el-tree>
    </div>
    <addDepts ref="addDept" :current-node-id="currentNodeId" :dialogshow.sync="dialogshow" @addDepts="getDepartmentList" />
  </div>
</template>
<script>
import { getDepartmentList, delDepartment } from '@/api/department'
import { listToTree } from '@/utils/listtotree'
import addDepts from './components/add-depts.vue'
export default {
  name: 'Department',
  components: { addDepts },
  data() {
    return {
      depts: [
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogshow: false,
      currentNodeId: ''
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    // 获取部门列表
    async getDepartmentList() {
      const res = await getDepartmentList()
      console.log(res)
      this.depts = listToTree(res, 0)
    },
    // 下拉菜单点击事件
    handleCommand(id, type) {
      console.log(id, type)
      if (type === 'add') {
        this.dialogshow = true
        this.currentNodeId = id // 获取到父级部门的id
      } else if (type === 'edit') {
        this.dialogshow = true
        this.currentNodeId = id // 获取到父级部门的id
        // 显示子组件，内部的数据和方法并没有完全构建完成
        // 父组件更新子组件的props ==> 异步的
        // 解决方案：
        // 让子组件完全构建完成再执行
        // 晚一点调用子组件的方法
        this.$nextTick(() => {
          // vue更新dom的过程是异步的
          // 批量的更新 - 提高更新性能
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        // 提示用户确认要删除吗
        this.$confirm('您确认要删除吗').then(async() => {
          await delDepartment(id)
          this.$message.success('删除成功')
          this.getDepartmentList()
        })
      }
    }
  }
}
</script>
<style scoped lang="scss">
.app-container{
  padding: 80px 120px;
}
.custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
  .right{
    display: flex;
    justify-content: space-between;
  }
</style>
